package com.chengxiaoxiao.dao.impl;

import com.chengxiaoxiao.dao.AppinfoDao;
import com.chengxiaoxiao.domain.Appinfo;
import com.chengxiaoxiao.utils.db.BeanHandler;
import com.chengxiaoxiao.utils.db.BeanListHandler;
import com.chengxiaoxiao.utils.db.DBAssist;
import com.chengxiaoxiao.utils.db.DBCPUtil;

import java.util.List;

public class AppinfoDaoImpl implements AppinfoDao {
    private DBAssist da = new DBAssist(DBCPUtil.getDataSource());

    @Override
    public void add(Appinfo appinfo) {
        Appinfo info =getLastVersion();
        da.update("insert into appinfo(name,updateContent,subTime,address,versionNum,iconAddress,versionCode) values(?,?,?,?,?,?,?)", appinfo.getName(), appinfo.getUpdateContent(),
                appinfo.getSubTime(), appinfo.getAddress(), appinfo.getVersionNum(), appinfo.getIconAddress(),info.getVersionCode());


    }

    @Override
    public void update(Appinfo appinfo) {
        da.update("update appinfo set name=?,updateContent=?,subTime=?,address=?,versionNum=?,iconAddress=? where id=?", appinfo.getName(), appinfo.getUpdateContent(),
                appinfo.getSubTime(), appinfo.getAddress(), appinfo.getVersionNum(), appinfo.getIconAddress(), appinfo.getId());
    }

    @Override
    public void delete(long id) {

        da.update("delete from appinfo where id=?", id);
    }

    @Override
    public List get() {
        return (List) da.query("select * from appinfo", new BeanListHandler(Appinfo.class));
    }

    @Override
    public Appinfo getbyId(long id) {

        return (Appinfo) da.query("select * from appinfo where id=?", new BeanHandler(Appinfo.class), id);
    }

    @Override
    public List getListPage(int startIndex, int pageSize) {
        return null;
    }

    @Override
    public Appinfo getLastVersion() {
        return (Appinfo) da.query("select * from appinfo order by id desc limit 1", new BeanHandler(Appinfo.class));
    }

    @Override
    public List getAll() {
        return (List) da.query("select * from appinfo order by id desc", new BeanListHandler(Appinfo.class));
    }
}
